<?php
/**
 * @title 863. 二叉树中所有距离为 K 的结点 - 递归
 * @author start2004
 */

// ini_set("memory_limit", "128M");

class Solution {
    /**
     * @param TreeNode $root
     * @param TreeNode $target
     * @param Integer $k
     * @return Integer[]
     */
    function distanceK($root, $target, $k) {
        /**
         * @since 2021-07-28 已经遍历的点
         */
        $this->existsArray = [];

        /**
         * @since 2021-07-28 存储每个点, 和他们的父节点
         */
        $nodeParentArray[$root->val] = [null, $root];

        /**
         * @since 2021-07-28 层次遍历
         */
        $nodeArray = [$root];
        while($nodeArray){
            $nexts = [];

            /**
             * @since 2021-07-28 遍历节点
             */
            foreach ($nodeArray as $node){
                if($node->left !== null){
                    $nodeParentArray[$node->left->val] = [$node, $node->left];
                    $nexts[] = $node->left;
                } else {}
                if($node->right !== null){
                    $nodeParentArray[$node->right->val] = [$node, $node->right];
                    $nexts[] = $node->right;
                } else {}
            }

            /**
             * @since 2021-07-28 下次迭代
             */
            $nodeArray = $nexts;
        }
        // return $nodeParentArray;

        /**
         * @since 2021-07-28 结果集
         */
        $this->result = [];

        /**
         * @since 2021-07-28 目标节点
         */
        $targetNode = $nodeParentArray[$target->val][1];
        while($k>=0){
            self::find($targetNode, $k);

            /**
             * @since 2021-07-28 父节点
             */
            $val = $targetNode->val;
            $this->existsArray[$val] = 1;
            $targetNode = $nodeParentArray[$val][0];
            if($targetNode == null){
                break;
            } else {}
            $k--;
        }

        /**
         * @return
         */
        return $this->result;
    }

    /**
     * @title 找node下子节点, 距离k
     * @author start2004
     * @since 2021-07-28 Created
     *
     * @param $node
     * @param int $k
     */
    public function find($node, $k=0){
        if($k == 0){
            $this->result[] = $node->val;
        } else {
            /**
             * @since 2021-07-28 层次遍历子节点
             */
            $nodeArray = [$node];
            while($nodeArray AND $k>0){
                $nexts = [];

                foreach ($nodeArray as $node){
                    if($node->left !== null AND !isset($this->existsArray[$node->left->val])){
                        $nexts[] = $node->left;
                    } else {}
                    if($node->right !== null AND !isset($this->existsArray[$node->right->val])){
                        $nexts[] = $node->right;
                    } else {}
                }

                $k--;
                $nodeArray = $nexts;
            }

            /**
             * @since 2021-07-28 收官
             */
            foreach ($nodeArray as $node){
                $this->result[] = $node->val;
            }
        }
    }
}

/**
 * @url http://127.0.0.1/leetcode/202107/2021.07.28.php
 */
$datas = [
    [[3,5,1,6,2,0,8,null,null,7,4], 5, 2], // 7,4,1
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    // var_dump($data);

    $obj = new Solution();
    $result = $obj->distanceK($xhprof->tree($data[0]), $data[1], $data[2]);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
